package com.platform.api;

import com.platform.annotation.LoginUser;
import com.platform.entity.ApiAmountBillVo;
import com.platform.entity.ApiCallBillVo;
import com.platform.entity.UserInfo;
import com.platform.entity.UserVo;
import com.platform.service.ApiAmountBillService;
import com.platform.service.ApiCallBillService;
import com.platform.service.ApiOrderService;
import com.platform.service.ApiUserService;
import com.platform.util.LotteryUtil;
import com.platform.util.UtilServiceName;
import com.platform.utils.Query;
import com.platform.utils.R;
import io.rong.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 通知
 *
 * @author zhurusen
 * @email 524227263@qq.com
 */
@RestController
@RequestMapping("/api/notice")
public class ApiNoticeController {

    @Autowired
    private ApiOrderService apiOrderService;
    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private ApiAmountBillService apiAmountBillService;
    @Autowired
    private ApiCallBillService apiCallBillService;


    /**
     * 通知 顶部参数
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("index")
    public R recharge(@LoginUser UserVo loginUser) {
        Map<String, Object> objectMap = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        if (1 == 1) {
            params.put("page", 1);
            params.put("limit", 1);
            params.put("userId", loginUser.getId());

            params.put("sidx", "create_time");
            params.put("order", "desc");
            Query query = new Query(params);


            List<ApiCallBillVo> list = apiCallBillService.selectList(query);
            if (list.size() > 0) {

                ApiCallBillVo info = list.get(0);
                //1接通 2未接通 3拒接
                String status = "已接通";
                if (info.getStatus() == 2L) {
                    status = "未接通话";
                }
                if (info.getStatus() == 3L) {
                    status = "拒接通话";
                }
                //获取：1拨出 2 接听
                String receiver = "";
                if (info.getMode() == 1L) {
                    UserInfo userVo = apiUserService.toUserInfo(info.getTvAnchor(), loginUser);
                    receiver = userVo.getNickName();
                }
                if (info.getMode() == 2L) {
                    UserInfo userVo = apiUserService.toUserInfo(info.getConsumer(), loginUser);
                    receiver = userVo.getNickName();
                }


                objectMap.put("call", status + ":" + receiver);
            } else {
                objectMap.put("call", "暂无通话记录");
            }
        }
        if (2 == 2) {
            params = new HashMap();
            params.put("page", 1);
            params.put("limit", 1);
            /**
             * 1 支出 2 收入
             */
            params.put("type", 1);
            params.put("status", 3);
            params.put("userId", loginUser.getId());

            params.put("sidx", "create_time");
            params.put("order", "desc");
            Query query = new Query(params);

            List<ApiAmountBillVo> list = apiAmountBillService.queryList(query);
            if (list.size() > 0) {
                objectMap.put("circle", "您刚支出了" + list.get(0).getAmount() + "窝币");
            } else {
                objectMap.put("circle", "您暂无支出");
            }
        }

        if (3 == 3) {
            params = new HashMap();
            params.put("page", 1);
            params.put("limit", 1);
            /**
             * 1 支出 2 收入
             */
            params.put("type", 2);
            params.put("status", 3);

            params.put("mouldName", "chongzhi");
            params.put("userId", loginUser.getId());

            params.put("sidx", "create_time");
            params.put("order", "desc");
            Query query = new Query(params);


            List<ApiAmountBillVo> list = apiAmountBillService.queryList(query);
            if (list.size() > 0) {

                objectMap.put("recharge", "您刚充值了" + list.get(0).getAmount() + "窝币");
            } else {
                objectMap.put("recharge", "您暂无充值");
            }
        }
        if (4 == 4) {
            params = new HashMap();
            params.put("cmUserId", loginUser.getId());
            Integer count = apiOrderService.selectCount(params);
            objectMap.put("orderNul", "我有" + count + "个预约");
        }

        return R.ok(0, "请求成功", objectMap);
    }


    /**
     * 探索
     *
     * @param loginUser
     * @param page
     * @return
     */
    @RequestMapping("tansuo")
    public R tansuo(@LoginUser UserVo loginUser, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);
        params.put("sidx", "create_time");
        params.put("order", "desc");
        params.put("type", 1);
        params.put("tansuo", true);
        params.put("userId", loginUser.getId());

        Query query = new Query(params);

        List<ApiAmountBillVo> list = apiAmountBillService.queryList(query);

        List<Map<String, Object>> rebObject = new ArrayList<>();
        for (ApiAmountBillVo info : list) {
            Map<String, Object> map = new HashMap<>();
            UserVo userVo = apiUserService.queryObject(info.getMouldId());
            UserInfo mouldUser = new UserInfo();
            mouldUser.setNickName(userVo.getNickname());
            mouldUser.setAvatar(userVo.getAvatar());
            map.put("mouldUserInfo", mouldUser);

            map.put("createTime", info.getCreateTime());

            map.put("amount", info.getAmount());
            map.put("mouldName", UtilServiceName.serviceNmae(info.getMouldName()));
            rebObject.add(map);
        }

        return R.ok(0, "获取成功", rebObject);
    }

    /**
     * 窝币
     *
     * @param loginUser
     * @param page
     * @return
     */
    @RequestMapping("recharge")
    public R recharge(@LoginUser UserVo loginUser, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);

        params.put("sidx", "create_time");
        params.put("order", "desc");
        params.put("recharge", true);

        params.put("userId", loginUser.getId());

        Query query = new Query(params);

        List<ApiAmountBillVo> list = apiAmountBillService.queryList(query);

        List<Map<String, Object>> rebObject = new ArrayList<>();
        for (ApiAmountBillVo info : list) {

            Map<String, Object> map = new HashMap<>();
            map.put("mouldName", info.getMouldName());
            map.put("createTime", info.getCreateTime());
            map.put("amount", info.getAmount());
            rebObject.add(map);
        }
        return R.ok(0, "获取成功", rebObject);
    }


}
